package com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.entity.SurTaskManage;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.queryParams.QueParams;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.repository.SurTaskManageRepository;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.vo.SurTaskManageVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class SurTaskManageRepositoryImpl implements SurTaskManageRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId,String type) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_survey_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        if (!PlatformObjectUtils.isEmpty(type)) {
            whereSql.append(" and type = '"+type+"'");
        }

        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SurTaskManage.class)).size();
        List<SurTaskManage> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SurTaskManage.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void save(SurTaskManage surTaskManage) {
        String sql = "insert into xczh_survey_task_manage(xstm_id,xsem_id,xsem_code,evaluation_subarea_name,evaluation_subarea_type,province,county,area," +
                "street,group_id,group_name,task_status,create_user,create_time,update_user,update_time,del_flag,create_user_name," +
                "evaluation_subarea_range,is_relation,evaluation_subarea_code,create_unit,type,geom,update_flag,si_id) " +
                "values(:xstmId, :xsemId, :xsemCode, :evaluationSubareaName, :evaluationSubareaType, :province, :county, :area, " +
                ":street, :groupId, :groupName, :taskStatus, :createUser, :createTime, :updateUser, :updateTime, :delFlag, :createUserName, " +
                ":evaluationSubareaRange,:isRelation,:evaluationSubareaCode,:createUnit,:type,ST_GeomFromText(:geom,4490),:updateFlag,:siId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(surTaskManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(SurTaskManage surTaskManage) {
        String sql = "update xczh_survey_task_manage set evaluation_subarea_name = :evaluationSubareaName, evaluation_subarea_type = :evaluationSubareaType, street = :street, " +
                "group_id= :groupId, group_name = :groupName ,update_user = :updateUser ,update_time = :updateTime, geom = ST_GeomFromText(:geom,4490),update_flag=:updateFlag where xstm_id = :xstmId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(surTaskManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public SurTaskManage findById(String id) {
        String sql = "select *,ST_AsText(geom) geom from xczh_survey_task_manage where xstm_id = ? ";
        List<SurTaskManage> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SurTaskManage.class),
                id);
        return query.size()<1?null:query.get(0);
    }

    @Override
    public void delete(String id) {
        String sql = "update xczh_survey_task_manage set del_flag= ? where xstm_id = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(), id);
    }

    @Override
    public String getMaxTaskCode(String code) {
        String sql="select max(task_code) from xczh_survey_task_manage where del_flag = '0' and task_code like '%"+code+"%'";
        String maxNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxNum;
    }

    @Override
    public String getMaxEvaSubCode(String code) {
        String sql="select max(evaluation_subarea_code) from xczh_survey_task_manage where del_flag = '0' and evaluation_subarea_code like '%"+code+"%'";
        String maxNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxNum;
    }

    @Override
    public List<SurTaskManage> getTaskListByXsemId(String xsemId) {
        String sql = "select * from xczh_survey_task_manage where xsem_id = ? and del_flag = ?";
        List<SurTaskManage> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SurTaskManage.class), xsemId, YNEnum.N.toString());
        return list;
    }

    @Override
    public Map<String, Object> getTaskListByXsemIdAndType(String xsemId,String type,int curPage,int pageSize) {
        StringBuilder sql = new StringBuilder("select * from xczh_survey_task_manage where xsem_id = '"+xsemId+"' and type = '"+type+"' and del_flag = '0' order by create_time");
        //String sql = "select * from xczh_survey_task_manage where xsem_id = ? and type = ? and del_flag = ?";
        //List<SurTaskManage> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SurTaskManage.class), xsemId, type,YNEnum.N.toString());
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SurTaskManage.class)).size();
        List<SurTaskManage> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SurTaskManage.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public List<SurTaskManage> getAllData(String xsemId,String type) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_survey_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        if (!PlatformObjectUtils.isEmpty(type)) {
            whereSql.append(" and type = '"+type+"'");
        }
        List<SurTaskManage> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(SurTaskManage.class));
        return list;
    }

    @Override
    public List<SurTaskManage> getAllDataByCode(String xsemId) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_survey_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
//        if (!PlatformObjectUtils.isEmpty(xsemCode)) {
//            whereSql.append(" and xsem_code = '"+xsemCode+"'");
//        }
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        List<SurTaskManage> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(SurTaskManage.class));
        return list;
    }

    @Override
    public List<SurTaskManage> getAllDefineDataByCode(String xsemId) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_survey_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0' and type='1'");
//        if (!PlatformObjectUtils.isEmpty(xsemCode)) {
//            whereSql.append(" and xsem_code = '"+xsemCode+"'");
//        }
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        List<SurTaskManage> list = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(SurTaskManage.class));
        return list;
    }

    @Override
    public void updateXsemId(String xsemId, String convertedXsemId) {
        String sql = "update xczh_survey_task_manage set xsem_id = '"+xsemId+"'  where xsem_id = '"+convertedXsemId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateTaskStatu(String xstmId, String taskStatus) {
        String sql = "update xczh_survey_task_manage set task_status = '"+taskStatus+"'  where xstm_id = '"+xstmId+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<String> getXstmIdsBYXsemId(String xsemId) {
        String sql ="select xstm_id from xczh_survey_task_manage where xsem_id = '"+xsemId+"' and  del_flag = '0' ";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public List<SurTaskManage> getTaskByTypeAndXsemId(String xsemId) {
        String sql ="select *,ST_AsText(geom) geom from xczh_survey_task_manage where xsem_id = '"+xsemId+"' and   del_flag = '0' ";
        return jdbcTemplate.query(sql,  new BeanPropertyRowMapper<>(SurTaskManage.class));
    }

    @Override
    public Map<String, Object> getAppPage(int curPage, int pageSize, String xsemId) {
        StringBuilder sql = new StringBuilder("select *,ST_AsText(geom) geom from xczh_survey_task_manage");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '"+xsemId+"'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SurTaskManageVo.class)).size();
        List<SurTaskManageVo> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(SurTaskManageVo.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }
}
